package com.travelsmart.client.apresentacao.view.subview;

import java.util.Date;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.travelsmart.client.apresentacao.i18n.I18nConstantes;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.view.BaseView;
import com.travelsmart.client.apresentacao.widget.FormDate;
import com.travelsmart.client.apresentacao.widget.FormSuggestBox;
import com.travelsmart.client.apresentacao.widget.FormWidget;
import com.travelsmart.client.bean.to.FilterPesquisaHotel;

public class ViewInfoPesquisaHotel extends BaseView {

	private final VerticalPanel vPanelViewInfoPesquisaHotel = new VerticalPanel();
	private final HTML msg = new HTML();

	private final FormSuggestBox fsb_local = new FormSuggestBox("fsb_local",
			I18nMessages.INSTANCE.help_destino(),
			I18nConstantes.INSTANCE.destino(),
			new FormWidget.HandlerValidation<String>() {
				@Override
				public boolean handle(String valor) {
					boolean retorno = true;
					if (valor == null || valor.trim().equals("")) {
						retorno = false;
						msg.setHTML(I18nMessages.INSTANCE
								.MSG_CAMPO_OBRIGATORIO(I18nMessages.INSTANCE
										.destino()));
					}
					return retorno;
				}
			});

	private final FormDate fd_entrada = new FormDate(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24),
			I18nMessages.INSTANCE.help_entrada(),
			new FormWidget.HandlerValidation<Date>() {

				@Override
				public boolean handle(Date valor) {
					boolean retorno = true;
					if (valor != null) {
						if (!valor.after(new Date())) {
							retorno = false;
							msg.setHTML(I18nMessages.INSTANCE
									.MSG_DATA_ENTRADA_ERR());
						}
					}
					return retorno;
				}

			});
	private final FormDate fd_saida = new FormDate(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 3),
			I18nMessages.INSTANCE.help_saida(),
			new FormWidget.HandlerValidation<Date>() {

				@Override
				public boolean handle(Date valor) {
					boolean retorno = true;
					if (valor != null) {
						if (!valor.after(new Date())) {
							retorno = false;
							msg.setHTML(I18nMessages.INSTANCE
									.MSG_DATA_SAIDA_ERR1());
						} else if (fd_entrada.getValue() == null) {
							retorno = false;
							msg.setHTML(I18nMessages.INSTANCE
									.MSG_CAMPO_OBRIGATORIO(I18nMessages.INSTANCE
											.entrada()));
						} else if (fd_entrada.getValue().after(valor)) {
							retorno = false;
							msg.setHTML(I18nMessages.INSTANCE
									.MSG_DATA_SAIDA_ERR2());
						}
					}
					return retorno;
				}
			});

	public ViewInfoPesquisaHotel() {
		super(new HTML(""));
		initWidget(vPanelViewInfoPesquisaHotel);
	}

	public void exec() {
		vPanelViewInfoPesquisaHotel.clear();
		vPanelViewInfoPesquisaHotel.add(msg);
		vPanelViewInfoPesquisaHotel.add(datasDestino());
	}

	public void carregarFilterPesquisaHotel(FilterPesquisaHotel filter) {
		carregarDataEntradaFilterPesquisaHotel(filter);
		carregarDataSaidaFilterPesquisaHotel(filter);
		filter.setLocal(fsb_local.getValue());
		fsb_local.clean();
		fd_entrada.clean();
		fd_saida.clean();

	}

	private void carregarDataSaidaFilterPesquisaHotel(FilterPesquisaHotel filter) {
		if (fd_saida.getValue() != null) {
			filter.setSaida(fd_saida.getValue());
		} else {
			filter.setSaida(new Date(System.currentTimeMillis()
					+ (2 * 24 * 60 * 60 * 1000)));
		}
	}

	private void carregarDataEntradaFilterPesquisaHotel(
			FilterPesquisaHotel filter) {
		if (fd_entrada.getValue() != null) {
			filter.setEntrada(fd_entrada.getValue());
		} else {
			filter.setEntrada(new Date(System.currentTimeMillis()
					+ (24 * 60 * 60 * 1000)));
		}
	}

	public boolean validar() {
		boolean retorno = false;
		if (fsb_local.validar() && fd_entrada.validar() && fd_saida.validar()) {
			retorno = true;
		}
		return retorno;
	}

	private Grid datasDestino() {
		Grid datasDestino = new Grid(4, 2);
		// Add a title to the form
		datasDestino.setHTML(0, 0, I18nMessages.INSTANCE.pesquisa_hotel());
		// Add some standard form options
		datasDestino.setHTML(1, 0, I18nMessages.INSTANCE.destino() + ":");
		datasDestino.setHTML(2, 0, I18nMessages.INSTANCE.entrada() + ":");
		datasDestino.setWidget(1, 1, fsb_local);
		datasDestino.setHTML(3, 0, I18nMessages.INSTANCE.saida() + ":");
		datasDestino.setWidget(2, 1, this.fd_entrada);
		datasDestino.setWidget(3, 1, this.fd_saida);
		formatDatasDestino(datasDestino);
		return datasDestino;
	}

	private void formatDatasDestino(Grid datasDestino) {
		datasDestino.setCellSpacing(6);
		datasDestino.setWidth("300px");
		datasDestino.getCellFormatter().setHorizontalAlignment(1, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(2, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(1, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		datasDestino.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
	}

}
